package com.atguigu.juc.one;

import java.util.concurrent.TimeUnit;

/**
 * 资源类 手机
 */
@SuppressWarnings("all")
class Phone {
    //发短信
    public static synchronized void sendSMS() throws Exception {
       // TimeUnit.SECONDS.sleep(4);//手中持有锁 进行睡觉
        System.out.println("------sendSMS");
    }
    //发邮件
    public synchronized void sendEmail() throws Exception {
        System.out.println("------sendEmail");
    }
    //微信聊天 打个招呼  普通
    public void getHello() {
        synchronized ("hahaha"){//同步代码块  锁是括号 this 当前对象
        }
        System.out.println("------getHello");
    }

}
public class Lock_8 {
    //多线程
    public static void main(String[] args) throws Exception {

        //一部手机
        Phone phone = new Phone();
        //另一部手机
        Phone phone2 = new Phone();

        new Thread(() -> {
            try {
                phone2.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "AA").start();

        Thread.sleep(100);//100ms

        new Thread(() -> {
            try {
                //phone.sendEmail();
                phone.getHello();
                //phone2.sendEmail();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "BB").start();
    }
}